package com.freesky.biz.activity.service.impl;

import com.freesky.biz.activity.domain.TAlipayCouponDetail;
import com.freesky.biz.activity.domain.TCouponDetail;
import com.freesky.biz.activity.domain.TStockMonitor;
import com.freesky.biz.activity.mapper.CouponDetailMapper;
import com.freesky.biz.activity.mapper.StockMonitorMapper;
import com.freesky.biz.activity.service.IActivityConfigService;
import com.freesky.biz.activity.service.ICouponDetailService;
import com.freesky.biz.activity.service.IStockMonitorService;
import com.freesky.biz.pay.service.ICouponStockService;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.domain.entity.SysUser;
import com.freesky.common.enums.ExportStatusEnum;
import com.freesky.common.utils.SecurityUtils;
import com.freesky.common.utils.StringUtils;
import com.freesky.system.domain.TExportRecord;
import com.freesky.system.service.ITExportRecordService;
import com.freesky.system.task.SyncReport;
import org.checkerframework.checker.signedness.qual.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class CouponDetailServiceImpl implements ICouponDetailService {

    @Autowired
    private CouponDetailMapper couponDetailMapper;

    @Autowired
    private StockMonitorMapper stockMonitorMapper;

    @Autowired
    private ICouponStockService couponStockService;

    @Autowired
    private IActivityConfigService activityConfigService;

    @Autowired
    private ITExportRecordService exportRecordService;

    @Autowired
    private SyncReport syncReport;

    @Override
    public List<TCouponDetail> findList(TCouponDetail couponDetail) {
        return couponDetailMapper.findList(couponDetail);
    }

    /**
     * 券的列表
     * @param busiId
     * @return
     */
    @Override
    public List<TCouponDetail> selectListByBusiId(String busiId) {
        return couponDetailMapper.selectListByBusiId(busiId);
    }

    @Override
    public AjaxResult exportFile(TCouponDetail couponDetail) {
        /*
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(Long.parseLong(SeqUtil.getSeqId()));
        exportRecord.setBizType("COUPON_DETAIL");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }

        exportRecord.setStatus(ExportStatusEnum.SUBMIT_FAIL.getValue());
        exportRecordService.insert(exportRecord);

        syncReport.exportFileMutilHeader(couponDetail, new TCouponDetail(), ICouponDetailService.class,
                "findList", "发券明细",exportRecord.getId(),sysUser.getUserName());

         */
        return AjaxResult.success();
    }

    @Override
    public AjaxResult grantCoupon(TCouponDetail couponDetail){
        TStockMonitor stockMonitor = new TStockMonitor();
        stockMonitor.setId(couponDetail.getStockId());
        List<TStockMonitor> stockMonitorList = stockMonitorMapper.findList(stockMonitor);
        if(stockMonitorList.size()>0){
            BigDecimal bigDecimal = new BigDecimal(stockMonitorList.get(0).getCouponAmount());
            couponDetail.setAmt(String.valueOf(bigDecimal.intValue() * 100));
        }

        AjaxResult ajaxResult = couponStockService.grantCouponsOpenId(couponDetail);
        couponDetail.setStatusMsg(ajaxResult.getMsg());
        couponDetailMapper.addCouponDetail(couponDetail);

        return ajaxResult;
    }

    @Override
    public AjaxResult grantAlipayCoupon(TAlipayCouponDetail couponDetail){

        AjaxResult ajaxResult = couponStockService.grantAlipayCoupon(couponDetail);
        if(ajaxResult.isSuccess()){
            TAlipayCouponDetail respCouponDetail = (TAlipayCouponDetail)ajaxResult.get("data");
            couponDetailMapper.addAliCouponDetail(couponDetail);
        }
        return ajaxResult;
    }

    @Override
    public List<TCouponDetail> findAlipayList(TCouponDetail entity) {
        return couponDetailMapper.findAlipayList(entity);
    }

    @Override
    public List<TCouponDetail> selectAlipayListByBusiId(String busiId) {
        return couponDetailMapper.selectAlipayListByBusiId(busiId);
    }
}
